package com.example.api.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWTUtil {


//    public static void main(String[] args) {
//
//        Map<String,Object> payloadInfo = new HashMap<>();
//        payloadInfo.put("name","zs");
//        payloadInfo.put("address","guangzhou");
//
//
//        String token = generateToken(payloadInfo,"123456",24*60*60);
//        System.out.println(token);
//
//
//        payloadInfo =  parseJWT(token,"123456");
//
//        System.out.println(payloadInfo.get("name"));
//        System.out.println(payloadInfo.get("address"));
//
//    }

    public static final String USER_ID = "user_id";
    public static final String CUSTOMER_ID = "customer_id";

    //对称加密


    public static String generateToken(Map<String,Object> claimInfo, String key, int expire)  {

        long expMillis = System.currentTimeMillis() + expire*1000;
        Date exp = new Date(expMillis);

        return Jwts.builder()
                .setClaims(claimInfo)
                .setExpiration(exp)
                .signWith(SignatureAlgorithm.HS256,key)
                .compact();
    }

    /**
     * 从token中获取原始信息
     * @param token
     * @param key
     * @return
     * @throws Exception
     */
    public static Map<String,Object> parseJWT(String token, String key)  {
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(key).parseClaimsJws(token);
        Claims body = claimsJws.getBody();
        return body;
    }
}
